# 作者：宋安康
# 开发时间：2023/11/19 15:51
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
mnist = fetch_openml('mnist_784', version=1)
X = mnist['data']
y = mnist['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
lr = LogisticRegression(solver='lbfgs', multi_class='ovr', max_iter=1000)
lr.fit(X_train, y_train)
score = lr.score(X_test, y_test)
print("Accuracy:", score)
fig, axes = plt.subplots(4, 4, figsize=(10, 10))
for i, ax in enumerate(axes.flat):
    ax.imshow(X_test[i].reshape(28, 28), cmap='gray')
    ax.set_title(f"Prediction: {lr.predict([X_test[i]])[0]}")
plt.show()
